package com.hnchances.weatherTest;

import org.json.JSONObject;

/**
 * @author: zyx
 * @created: 2025-10-08-19:06
 * @description:
 */
public class Demo {
    public static void main(String[] args) throws Exception {
        try {
            String weatherJson = QWeatherService.getWeather("101210101");

            JSONObject jsonObject = new JSONObject(weatherJson);

            // 检查API调用是否成功
            String code = jsonObject.getString("code");
            if (!"200".equals(code)) {
                System.out.println("API调用失败，错误码: " + code);
                return;
            }

            // 解析并输出天气数据
            JSONObject now = jsonObject.getJSONObject("now");

            System.out.println("=== 天气信息 ===");
            System.out.println("温度: " + now.getString("temp") + "°C");
            System.out.println("天气: " + now.getString("text"));
            System.out.println("体感温度: " + now.getString("feelsLike") + "°C");
            System.out.println("风向: " + now.getString("windDir"));
            System.out.println("风力等级: " + now.getString("windScale") + "级");
            System.out.println("湿度: " + now.getString("humidity") + "%");
            System.out.println("气压: " + now.getString("pressure") + "hPa");
            System.out.println("网址链接: " + jsonObject.getString("fxLink"));
            System.out.println("更新时间: " + jsonObject.getString("updateTime"));


        } catch (Exception e) {
            System.err.println("获取天气数据失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
